<?php
namespace Admin\Controller;
use Admin\Controller\CheckController;
use Admin\Controller\ExpressController;
class PledgeController extends AdminController{
	//抵押资料
    protected function _initialize() {
		parent::_initialize ();
	}
	public $express_company = [1=>'顺丰快递','圆通快递','中通快递','申通快递','韵达快递'];//快递公司
//抵押备案资料
	public function record_filing()
  {
	    // $info=new CheckController();
	    // $list=$info->getCheck_status(17,10);
	    // $this->assign('list',$list);
		$this->display();
	}
	//一次归档
	public function An_archive()
  {
		$info=new CheckController();
	  $list=$info->getCheck_status(17,'!=-1');
	  $this->assign('list',$list);
		$this->display();
	}

		//公证书列表
	public function Notarization()
  {
    $info=new CheckController();
	  $list=$info->getCheck_status(19,'!=-1');
	  $this->assign('list',$list);
		$this->display();
	}
	//寄出快递
	public function add_express()
  {
		$info=new CheckController();
	    $list=$info->getCheck_status(21,'!=-1');
	    foreach ($list as $key => $value) {
	    	$group=$this->getInfo('*','group','id='.$value['gpid']);
			$list[$key]['group_name']=$group['name'];
	    	$express=$this->getInfo('*','express','user_id='.$value['id'].' and status=4');
            if(!$express)
            {
             	$arr[$key]['id']=$value['id'];
                $arr[$key]['entry_no']=$value['entry_no'];
                $arr[$key]['user_name']=$value['user_name'];
                $arr[$key]['identity_no']=$value['identity_no'];
                $arr[$key]['telephone']=$value['telephone'];
                $arr[$key]['group_name']=$group['name'];
                $arr[$key]['create_time']=$value['create_time'];
                $arr[$key]['update_time']=$value['update_time'];
                $arr[$key]['bank_short_name']=$value['bank_short_name'];
             }
	    }
    $this->assign('express_company',$this->express_company);
	  $this->assign('list',$arr);
		$this->display();
	}
	//快递内容
	public function express_info()
  {
           $id=explode(',',$_POST['ids']);
           $tems  = 0;
           foreach ($id as $v) {
             if($v!=0)
             {
                  $user=$this->getInfo('*','user','id='.$v.' and check_status!=22');
                  if($user)
                  {
                   $data['user_id']=$v;
                   $data['user_name']=$user['user_name'];
                   $data['admin_id']=session('adminInfo.id');
                   $data['send_name']=$_POST['send_name'];
                   $data['addressee']=$_POST['addressee'];
                   $data['express_number']=$_POST['express_number'];
                   $data['file']=implode(',',$_POST['file']);
                   $data['company_id']=$_POST['company_id'];
                   $express=new ExpressController();
                   $data['express_company']=$express->express_company($_POST['company_id']);
                   $data['express_times']=strtotime($_POST['express_times']);
                   $data['add_times']=time();
                   $data['update_times']=time();
                   $data['status']=4;
                   $result=M('express')->add($data);

                   $log=array('user_id'=>$v,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>102,'opinion'=>'快递已录入(抵押岗)','note'=>'快递已录入(抵押岗)','add_time'=>time(),'check_status'=>22,'express_id'=> $result,'send_name'=>$_POST['send_name'],'addressee'=>$_POST['addressee'],'user_name'=>$user['user_name'],'express_number'=>$_POST['express_number'],'file'=>implode(',',$_POST['file']),'express_times'=>strtotime($_POST['express_times']));
                   M('check_log')->add($log);
                   $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'check_status'=>22);
                   M('user')->where('id='.$v)->save($check_data);
                 }else{
                    $this->error('该文件已寄出');
                 }
             }
                $tems++;

           }
            $this->success("批量执行成功");

	}
//银行抵押合同列表
	// public function Bank_Contract()  {
	// 	$info=new CheckController();
	//     $list=$info->getCheck_status(25,10);
	//     $this->assign('list',$list);
	// 	$this->display();
	// }

	//抵押资料补录(列表)
	public function add_index()
  {
		$info=new CheckController();
	  $list=$info->getCheck_status(23,'!=-1');
    foreach ($list as $key => $value) {
      $pledge=$this->getInfo('*','pledge','user_id='.$value['id']);
      if($pledge)
      {
          $list[$key]['pledge_status']=1;
      }else{
          $list[$key]['pledge_status']=0;
      }
    }
    $this->assign('express_company',$this->express_company);
	  $this->assign('list',$list);
		$this->display();
	}

   //抵押资料补录
   public function add_info()
   {
	   	if(IS_POST)
      {
	   	   $user_id=$_POST['user_id'];
         $pledgeInfo=$this->getInfo('*','pledge','user_id='.$user_id);
         if($pledgeInfo)
         {
           $this->error('抵押资料已补录',U('Pledge/add_index'),0);
         }
	       $_POST=array(
              'user_id'=>$user_id,
              'certificate'=>$_POST['certificate'],
              'plate_number'=>$_POST['plate_number'],
              'pledge_time'=>strtotime($_POST['pledge_time']),
              'add_time'=>time(),
              'status'=>1
	       	);
          $upload = new \Think\Upload();// 实例化上传类
          $upload->maxSize   = 3145728 ;// 设置附件上传大小
          $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
          $upload->rootPath  = './Uploads/'; // 设置附件上传根目录
          $upload->savePath  = ''; // 设置附件上传（子）目录
          $info =   $upload->upload();
          if (!isset($info['img_warranty1'])) {
               $this->error('保单1没上传');
               exit;
          }else if (!isset($info['img_warranty2'])) {
              $this->error('保单2没上传');
              exit;
          }else if (!isset($info['img_register1'])) {
              $this->error('登记证书1没上传');
              exit;
          }else if (!isset($info['img_register2'])) {
              $this->error('登记证书2没上传');
              exit;
          }else if (!isset($info['img_license'])) {
              $this->error('驾驶证没上传');
              exit;
          }
          if ($info)
          {
            // 上传成功 获取上传文件信息
              foreach($info as $uploadKey => $file)
              {
                $_POST[$uploadKey] = "http://".$_SERVER['HTTP_HOST']."/Uploads/".$file['savepath'].$file['savename'];
              }
          }
         //抵押补录
	       $pledge=$this->getInfo('*','pledge','user_id='.$user_id);
	       if(!$pledge)
         {
             $result=M('pledge')->add($_POST);
             $log=array('user_id'=>$user_id,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>103,'opinion'=>'抵押资料已补录','note'=>'抵押资料已补录','add_time'=>time(),'check_status'=>24);
             M('check_log')->add($log);
	       }else{
	        	$result=M('pledge')->where('user_id='.$user_id)->save($_POST);
	       }
	      if($result)
        {
            $apply=array('status'=>10,'update_time'=>time());
                M('user')->where('id='.$user_id)->save($apply);
             redirect('/Admin/Pledge/add_index');
	       }else{
	       	 $this->error('资料补录失败');
	       }
	   	}else{
	   		$user_id=I('get.user_id',0,'intval');
  			$user=$this->getInfo('*','user','id='.$user_id);
  			$info=$this->getInfo('*','express','user_id='.$user_id.' and status=5');
  			$pledge=$this->getInfo('*','pledge','user_id='.$user_id);
  	       	$info['file'] = explode(',', $info['file']);
  			$this->assign('info',$info);
  			$this->assign('pledge',$pledge);
  			$this->assign('express_company',$this->express_company);
		    $this->assign('user',$user);
	   		$this->display();
	   	}
   	}

   	public function add_expressInfo()
    {
          $id=explode(',',$_POST['ids']);
           $tems  = 0;
           foreach ($id as $v) {
             if($v!=0)
             {
                  $pledge=$this->getInfo('*','pledge','user_id='.$v);
                  if(!$pledge)
                  {
                    $this->error('有资料未补录，请补录完成后寄出');
                  }
                  $user=$this->getInfo('*','user','id='.$v.' and check_status!=24');
                  if($user)
                  {
                   $data['user_id']=$v;
                   $data['user_name']=$user['user_name'];
                   $data['admin_id']=session('adminInfo.id');
                   $data['send_name']=$_POST['send_name'];
                   $data['addressee']=$_POST['addressee'];
                   $data['express_number']=$_POST['express_number'];
                   $data['file']=implode(',',$_POST['file']);
                   $data['company_id']=$_POST['company_id'];
                   $express=new ExpressController();
                   $data['express_company']=$express->express_company($_POST['company_id']);
                   $data['express_times']=strtotime($_POST['express_times']);
                   $data['add_times']=time();
                   $data['update_times']=time();
                   $data['status']=6;
                   $result=M('express')->add($data);

                   $log=array('user_id'=>$v,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>102,'opinion'=>'快递已录入(渠道内勤)','note'=>'快递已录入(渠道内勤)','add_time'=>time(),'check_status'=>24,'express_id'=> $result,'send_name'=>$_POST['send_name'],'addressee'=>$_POST['addressee'],'user_name'=>$user['user_name'],'express_number'=>$_POST['express_number'],'file'=>implode(',',$_POST['file']),'express_times'=>strtotime($_POST['express_times']));
                   M('check_log')->add($log);
                   $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'check_status'=>24);
                   M('user')->where('id='.$v)->save($check_data);
                 }else{
                    $this->error('该文件已寄出');
                 }
             }
                $tems++;

           }
            $this->success("批量执行成功");
   	}

   	//公司归档资料转交
   	// public function archive_deliver()    {
   	// 	$info=new CheckController();
	   //  $list=$info->getCheck_status(26,10);
	   //  $this->assign('list',$list);
		  // $this->display();
   	// }

   		//快递确认
   	public function express_confirm()
    {
   		// $info=new CheckController();
	    // $list=$info->getCheck_status(24,10);
      $where = '1 = 1';
      // search
      $where .= I('get.user_name',false,'trim') != '' ? ' and user_name like "%'.I('get.user_name',false,'trim').'%"' : '';
      $where .= I('get.identity_no',false,'trim') != '' ? ' and identity_no like "%'.I('get.identity_no',false,'trim').'%"' : '';
      $where .= I('get.department_name',false,'trim') != '' ? ' and department_name like "%'.I('get.department_name',false,'trim').'%"' : '';
      // if(session('adminInfo.gpid')!=0){
      //  $where.=' and gpid='.session('adminInfo.gpid');
      // }
      $where.=' and check_status=24 and (u.status=9 or u.status=10)';
      $list=$this->getList('u.*,b.bank_short_name','user u',$where,'',true,'left join app_bank b on u.loan_bank_id=b.id');
      foreach ($list as $key => $value) {
        $group=$this->getInfo('*','group','id='.$value['gpid']);
        $list[$key]['group_name']=$group['name'];
      }
	    $this->assign('list',$list);
		  $this->display();
   	}

   	//快递确认详情

   public function confirm_info()
   {
      if(IS_POST)
      {

          $express=$this->getInfo('*','express','user_id='.$_POST['user_id'].' and status=6');
          if(!$express)
          {
            $this->error('不在该操作状态中',U('Pledge/express_confirm'));
          }
          if($express['confirm_file']!='')
          {
              $data['confirm_file']=$express['confirm_file'].','.implode(',',$_POST['file']);
           // $data['confirm_file']=$express['confirm_file'].','.implode(',',$_POST['file']);
          }else{
            $data['confirm_file']=implode(',',$_POST['file']);
          }
          if($_POST['confirm_type']==1)
          {
            $data['status']=6;
          }else{
            $data['status']=12;
          }
          $data['recipient'] =$_POST['recipient'];
          $data['update_time']=time();
          $result=M('express')->where('user_id='.$_POST['user_id'].' and status=6')->save($data);

          if($_POST['confirm_type']==0)
          {
               $log=array('user_id'=>$_POST['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'快递确认(抵押岗)','note'=>'快递确认(抵押岗)','add_time'=>time(),'file'=>implode(',',$_POST['file']),'check_status'=>26);
               M('check_log')->add($log);
               $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'status'=>10,'check_status'=>26);
               M('user')->where('id='.$_POST['user_id'])->save($check_data);
               redirect('/Admin/Pledge/express_confirm');
         }else{
            $log=array('user_id'=>$_POST['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'快递确认待定(抵押岗)','note'=>$_POST['note'],'add_time'=>time(),'file'=>implode(',',$POST['file']));
             M('check_log')->add($log);
             $check_data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'status'=>9);
             M('user')->where('id='.$_POST['user_id'])->save($check_data);
             $this->error('快递确认待定',U('Pledge/express_confirm'),0);
         }

      }else{
         $user_id=I('get.user_id',0,'intval');
         $info=$this->getInfo('*','express','user_id='.$user_id.' and status=6');
         $info['confirm_file'] = explode(',', $info['confirm_file']);
         $user=$this->getInfo('*','user','id='.$user_id);
         $this->assign('user',$user);
         $this->assign('info',$info);
         $this->display();
      }

    }

}